// pages/shopDetail/shopDetail.js
var app = getApp();
var getSign = require('../../utils/getSign/util.js');
var WxParse = require('../../utils/wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl: '',
    spec1:0,
    spec2: 0,
    currentSwiper: 0,
    isShowPay: false,
    goodsInfo: {
      norm: '',
      id:'',
      taochan:'',
      nianxian:'',
      info:{},
      numb: 1,
      SpecListVaule:''
    },
    Id:'',
    Is_Collection:false,
    Original_Price:'',
    Pic_List:[],
    Present_Price:'',
    Product_Brief:'',
    Product_Describe:'',
    Product_Lable:[],
    Product_Title:'',
    Spec_List:[],
    Spec_List_Vaule:[],
    Original_Price_Value:0,
    Present_Price_Value:0,
    iconPic:'',
    selectgg:'请选择规格'
  },
  addguige:function(e){
    // console.log(e)
    // console.log(e.currentTarget.dataset.pid)
    // console.log(e.target.dataset.icon)
    // console.log(e.target.dataset.itemid)
    let info = "goodsInfo.info"
    let SpecListVaule = "goodsInfo.SpecListVaule"
    let pid = e.currentTarget.dataset.pid
    var infomap = this.data.goodsInfo.info
    infomap[pid] = e.target.dataset.itemid
    var icon;
    if (e.target.dataset.icon == '') {
      icon = this.data.Pic_List[0]
    } else {
      icon = e.target.dataset.icon
    }
    this.setData({
      iconPic: icon,
      [info]: infomap,
    })
    console.log(this.data.goodsInfo.info)
    var Spec_List = this.data.Spec_List
    var infovalidate = this.data.goodsInfo.info
    var array =[]
    for (var i = 0; i < Spec_List.length;i++){
      var key = Spec_List[i].Spec_Id
      if (infovalidate[key]){
        array.push(infovalidate[key])
      }
    }
    this.setData({
      [SpecListVaule]: '[' + array.join()+']'
    })
    console.log(this.data.goodsInfo.SpecListVaule)
    var specValue = this.data.goodsInfo.SpecListVaule
    var listValue = this.data.Spec_List_Vaule
    for (var i = 0; i < listValue.length; i++) {
      var item = listValue[i];
      if (item.Spec_Item_List == specValue) {
        this.setData({
          Original_Price_Value: item.Original_Price,
          Present_Price_Value: item.Present_Price,
          selectgg: item.SKU_Txt
        })
      }
    }

  },

  //数量加1
  jia:function(){
    let numb = "goodsInfo.numb"
    let num = this.data.goodsInfo.numb
    this.setData({
      [numb] : num + 1
    })
  },
  //数量减1
  jian:function(){
    let numb = "goodsInfo.numb"
    let num = this.data.goodsInfo.numb
    if(num > 1){
      this.setData({
        [numb]: num - 1
      })
    }
  },
  //关闭支付弹窗
  close:function(){
    this.setData({
      isShowPay: false
    })
  },
   //打开支付弹窗
  payShow: function(){
    this.setData({
      isShowPay: true
    })
  },
  swiperChange: function (e) {
    this.setData({
      currentSwiper: e.detail.current
    })
  },
  //跳转到收藏
  goToCollection: function(){
    var memberInfo = wx.getStorageSync('memberInfo')
    var postArgs = {
      'Api_Type': '006',
      'product_id': this.data.Id,
      'Member_Id': memberInfo.MemberId,
      'Member_Token': memberInfo.MemberToken
    }
    //获取
    getSign.request('https://wx-app-api.sjune.cn/yuren/API.ashx', 'POST', postArgs, '').then(
      (res) => {
        console.log(res, '收藏')
        if (res.Status == 2) {
          wx.clearStorage({
            success: () => {
              wx.switchTab({
                url: '/pages/index/index',
              })
            }
          })
        } else if (res.Status == 0) {
          wx.showToast({
            title: res.Message,
            icon: 'success',
            duration: 2000
          })
          this.setData({
            Is_Collection: !this.data.Is_Collection
          })
        }
       
      },
      (err) => {
        console.log(err.Message, '收藏失败')
      }
    )
  },
  goToOrderBefore: function(){
    let id = "goodsInfo.id"
    this.setData({
      [id]: this.data.Id,
    })
    app.goodsInfo = this.data.goodsInfo
    
    // console.log(this.data.Spec_List.length,'Spec_List')
    var objarr = Object.keys(this.data.goodsInfo.info)
    // console.log(objarr.length, 'goodsInfo')
    if (this.data.Spec_List.length == objarr.length){
      wx.navigateTo({
        url: '/pages/orderBefore/orderBefore',
      })
    }
    else{
      wx.showToast({
        title: '选择套餐规格',
        duration: 2000
      })
    }
   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.id) 
    this.setData({
      baseUrl: app.baseUrl
    })
    var that = this;
    //检查Session_key是否过期
    wx.checkSession({
      success: (res) => {
        //未过期
        console.log('session_key shop页面未过期')
        //是否有数据
        if (true) {
          //没有数据 请求(apitype, pageIndex, classId, keys)
          this.requestPost('005', options.id, that)
        }
      },
      fail: (res) => {
        //过期
        wx.showToast({
          title: '检测权限',
          icon: 'loading',
          duration: 500
        })
        setTimeout(function () {
          wx.switchTab({
            url: '/pages/login/login',
          })
        }, 500)
      }
    })
  },
  //发送请求
  requestPost: function (apitype, productid, that) {
    var memberInfo = wx.getStorageSync('memberInfo')
    var postArgs = {
      'Api_Type': apitype,
      'product_id': productid,
      'Member_Id': memberInfo.MemberId,
      'Member_Token': memberInfo.MemberToken
    }
    //获取
    getSign.request('https://wx-app-api.sjune.cn/yuren/API.ashx', 'POST', postArgs, '').then(
      (res) => {
        console.log(res, '成功列表')
        this.setData({
          Id: res.Data.Id,
          Is_Collection: res.Data.Is_Collection,
          Original_Price: res.Data.Original_Price,
          Pic_List: res.Data.Pic_List,
          Present_Price: res.Data.Present_Price,
          Product_Brief: res.Data.Product_Brief,
          Product_Describe: res.Data.Product_Describe,
          Product_Lable: res.Data.Product_Lable,
          Product_Title: res.Data.Product_Title,
          // selectgg: res.Data.Product_Title,
          Spec_List: res.Data.Spec_List,
          Spec_List_Vaule: res.Data.Spec_List_Vaule,
          Original_Price_Value: res.Data.Original_Price,
          Present_Price_Value: res.Data.Present_Price,
          iconPic: res.Data.Pic_List[0]
        })
        var content = res.Data.Product_Describe;
        WxParse.wxParse('content', 'html', content, that, 5);
      },
      (err) => {
        console.log(err.Message, '成功列表失败')
      }
    )
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.invoiceid = 0
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})